package com.appspot.didit.client.workitem;

import java.util.Date;
import com.appspot.didit.client.tag.*;
import com.appspot.didit.client.util.Util;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.datepicker.client.DateBox;

public class CreateWorkitemView extends Composite implements CreateWorkitemPresenter.View {
	
	protected TextBox description;
	protected TextBox duration;
	protected TextBox tag;
	protected DateBox date;
	protected Label hint;
	protected Button saveButton;
	protected TextBox hour;
	protected TextBox minute;
	
    public CreateWorkitemView() {
    	
    	hint = new Label();
    	hint.setStylePrimaryName("hints");
    	
    	description = new TextBox();
        duration = new TextBox();
        tag = new TextBox();
        tag.addFocusHandler(new FocusHandler() {
			public void onFocus(FocusEvent event) {
				TagCloud tc = TagManager.getInstance().getTagCloud();
				if(tc != null && !tc.isShowing()) {
					tc.show(tag);
				}
			}
		});
        tag.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				tag.setStyleName("");
				
			}
		});
        tag.addKeyDownHandler(new KeyDownHandler() {
			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_TAB) {
					 TagCloud tc = TagManager.getInstance().getTagCloud();
						if(tc != null && tc.getPartner().equals(tag)) {
							tc.hide();
						}
				 }
			}
        	
        });
        date = new DateBox();
		date.setValue(new Date());
		date.setFormat(new DateBox.DefaultFormat(Util.completeDate));
		date.setStyleName("tb-noborder");
		date.setWidth("70px");//"120px"
		
		saveButton = new Button("Speichern");
		FlexTable layout = new FlexTable();
	    layout.setCellSpacing(6);
	    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

	    // Add a title to the form
	    layout.setHTML(0, 0, "Neues Workitem anlegen");
	    cellFormatter.setColSpan(0, 0, 4);
	    cellFormatter.setHorizontalAlignment(0, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);

	    layout.setHTML(1, 0, "Projekte");
	    layout.setHTML(1, 1, "Was wurde gemacht:");
	    layout.setHTML(1, 2, "Dauer");
	    layout.setHTML(1, 3, "Datum");
	    layout.setWidget(2, 0, hint);
	    cellFormatter.setColSpan(2, 0, 4);
	    cellFormatter.setHorizontalAlignment(2, 0,
	        HasHorizontalAlignment.ALIGN_CENTER);
	    layout.setWidget(3, 0, tag);
	    layout.setWidget(3, 1, description);
	    
	    HorizontalPanel dauer = new HorizontalPanel();
	    hour = new TextBox();
	    hour.setStylePrimaryName("tb-noborder");
	    hour.setValue("0");
	    hour.setMaxLength(3);
	    hour.setWidth("30");
	    hour.setTextAlignment(TextBoxBase.ALIGN_RIGHT);
	    hour.addValueChangeHandler(new ValueChangeHandler<String>() {

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				if(event.getValue().equals("")) {
					hour.setValue("0");
					return;
				}
				try {
					Integer.parseInt(event.getValue());
				} catch(Exception e) {
					hour.setValue("0");
				}
			}
	    	
	    });
	    minute = new TextBox();
	    minute.setStylePrimaryName("tb-noborder");
	    minute.setValue("00");
	    minute.setMaxLength(3);
	    minute.setWidth("30");
	    minute.setTextAlignment(TextBoxBase.ALIGN_LEFT);
	    minute.addValueChangeHandler(new ValueChangeHandler<String>() {

			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				if(event.getValue().equals("")) {
					minute.setValue("00");
					return;
				}
				try {
					if(Integer.parseInt(event.getValue()) < 10) {
						minute.setValue("0" + Integer.parseInt(event.getValue()));
					}
				} catch(Exception e) {
					minute.setValue("00");
				}
			}
	    	
	    });
	    dauer.add(hour);
	    dauer.add(new HTML(":"));
	    dauer.add(minute);
	    dauer.setStylePrimaryName("dauer");
	    layout.setWidget(3, 2, dauer);
	    layout.setWidget(3, 3, date);

	    layout.setWidget(3, 4, saveButton);
       
        initWidget(layout);
    }

    public Widget getWidget() {
            return this;
    }

	public void reset() {
		description.setValue("");
        duration.setValue("");
        tag.setValue("");
        hour.setValue("0");
        minute.setValue("00");
        hint.setText("");
	}
	
	public HasClickHandlers getSaveButtonClickHandlers() {
		return saveButton;
	}

	@Override
	public HasValue<Date> getDate() {
		return date;
	}

	@Override
	public HasValue<String> getDescription() {
		return description;
	}

	@Override
	public HasValue<String> getDuration() {
		return duration;
	}



	@Override
	public HasValue<String> getTag() {
		return tag;
	}

	public HasText getHint() {
		return hint;
	}
	
	public HasKeyPressHandlers getKeyPressHandler() {
		return duration;
	}

	@Override
	public HasValue<String> getHour() {
		return hour;
	}

	@Override
	public HasValue<String> getMinute() {
		return minute;
	}

}
